<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <a-form
        :model="where"
        :label-col="{md: {span: 6}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 18}, sm: {span: 24}}">
        <a-row>
              
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="标题名称:">
              <a-input
                v-model:value.trim="where.name"
                placeholder="请输入标题名称"
                allow-clear/>
            </a-form-item>
          </a-col>
              
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item style="padding-left:10px;" :wrapper-col="{span: 24}">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表格 -->
      <ele-pro-table
        ref="table"
        row-key="id"
        :datasource="url"
        :columns="columns"
        :where="where"
        v-model:selection="selection"
        :scroll="{x: 'max-content'}">
        <template #toolbar>
          <a-space>
            <a-button type="primary" @click="openEdit()" v-if="permission.includes('sys:commonregions:add')">
              <template #icon>
                <plus-outlined/>
              </template>
              <span>新建</span>
            </a-button>
            <a-button type="danger" @click="removeBatch" v-if="permission.includes('sys:commonregions:dall')">
              <template #icon>
                <delete-outlined/>
              </template>
              <span>删除</span>
            </a-button>
          </a-space>
        </template>
                  
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                  
        <!-- 操作栏 -->
        <template #action="{ record }">
          <a-space>
            <a @click="openEdit(record)" v-if="permission.includes('sys:commonregions:edit')">修改</a>
            <a-divider type="vertical"/>
            <a-popconfirm
              title="确定要删除此通用区域吗？"
              @confirm="remove(record)">
              <a class="ele-text-danger" v-if="permission.includes('sys:commonregions:delete')">删除</a>
            </a-popconfirm>
          </a-space>
        </template>
      </ele-pro-table>
    </a-card>
  </div>
  <!-- 编辑弹窗 -->
  <commonregions-edit
    v-model:visible="showEdit"
    :data="current"
    @done="reload"/>
</template>

<script>
import { mapGetters } from "vuex";
import {createVNode} from 'vue'
import {
  PlusOutlined,
  DeleteOutlined,
  ExclamationCircleOutlined
} from '@ant-design/icons-vue';
import CommonRegionsEdit from './commonregions-edit';

export default {
  name: 'ExamCommonRegions',
  components: {
    PlusOutlined,
    DeleteOutlined,
    CommonRegionsEdit
  },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/commonregions/index',
      // 表格列配置
      columns: [
        {
          key: 'index',
          title: '编号',
          width: 48,
          align: 'center',
          fixed: 'left',
          customRender: ({index}) => this.$refs.table.tableIndex + index
        },
            
        {
          title: '',
          dataIndex: 'parent_id',
          align: 'center'
        },
                      
        {
          title: '',
          dataIndex: 'lft',
          align: 'center'
        },
                      
        {
          title: '',
          dataIndex: 'rgt',
          align: 'center'
        },
                      
        {
          title: '',
          dataIndex: 'depth',
          align: 'center'
        },
                      
        {
          title: '',
          dataIndex: 'scope',
          align: 'center'
        },
                      
        {
          title: '唯一代码标识',
          dataIndex: 'code',
          align: 'center'
        },
                      
        {
          title: '标识别名',
          dataIndex: 'code_alias',
          align: 'center'
        },
                      
        {
          title: '标题名称',
          dataIndex: 'name',
          align: 'center'
        },
                      
        {
          title: '标题名称英文',
          dataIndex: 'name_en',
          align: 'center'
        },
                      
        {
          title: '创建时间',
          dataIndex: 'created_at',
          align: 'center'
        },
                      
        {
          title: '创建者名称',
          dataIndex: 'created_name',
          align: 'center'
        },
                      
        {
          title: '创建者id',
          dataIndex: 'created_by',
          align: 'center'
        },
                      
        {
          title: '更新时间',
          dataIndex: 'updated_at',
          align: 'center'
        },
                      
        {
          title: '更新者名称',
          dataIndex: 'updated_name',
          align: 'center'
        },
                      
        {
          title: '更新者id',
          dataIndex: 'updated_by',
          align: 'center'
        },
                      
        {
          title: '禁用时间戳',
          dataIndex: 'disabled_at',
          align: 'center'
        },
                      
        {
          title: '禁用者id',
          dataIndex: 'disabled_by',
          align: 'center'
        },
                      
        {
          title: '禁用者名称',
          dataIndex: 'disabled_name',
          align: 'center'
        },
                      
        {
          title: '说明备注',
          dataIndex: 'description',
          align: 'center'
        },
            
        {
          title: '创建时间',
          dataIndex: 'create_time',
          sorter: true,
          align: 'center',
          customRender: ({text}) => this.$util.toDateString(text)
        },
        {
          title: '更新时间',
          dataIndex: 'update_time',
          sorter: true,
          align: 'center',
          customRender: ({text}) => this.$util.toDateString(text)
        },
        {
          title: '操作',
          key: 'action',
          width: 100,
          align: 'center',
          fixed: 'right',
          slots: {customRender: 'action'}
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false
    };
  },
  methods: {
    /* 搜索 */
    reload() {
      this.selection = [];
      this.$refs.table.reload({page: 1});
    },
    /*  重置搜索 */
    reset() {
      this.where = {};
      this.$nextTick(() => {
        this.reload();
      });
    },
    /* 删除单个 */
    remove(row) {
      const hide = this.$message.loading('请求中..', 0);
      this.$http.post('/commonregions/delete', {id : row.id}).then(res => {
        hide();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        hide();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm({
        title: '提示',
        content: '确定要删除选中的通用区域吗?',
        icon: createVNode(ExclamationCircleOutlined),
        maskClosable: true,
        onOk: () => {
          const hide = this.$message.loading('请求中..', 0);
          this.$http.post('/commonregions/delete', {
            id: this.selection.map(d => d.id)
          }).then(res => {
            hide();
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            hide();
            this.$message.error(e.message);
          });
        }
      });
    },
    /* 打开编辑弹窗 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
                                                                                                                    
  }
}
</script>

<style scoped>
</style>
